Skip to content

Conversation

@bahusoid
Copy link
Member

Fixes #3156

@fredericDelaporte fredericDelaporte added this to the 5.4 milestone Sep 12, 2022
Comment on lines 66 to 74
[Test]
public void NullableNullValue()
{
int? v = null;
Expression<Func<int>> expression = () => v.Value;

Assert.Throws<InvalidCastException>(() => GetValue(expression));
Assert.Throws<InvalidCastException>(() => expression.Compile().Invoke());
}
Copy link
Member

@fredericDelaporte fredericDelaporte Sep 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about e => v.HasValue || e.SomeProperty = v.Value? I wonder if it is supported or if it would throw.

(There is not much value in supporting this, excepted "feature parity" with linq-to-object: the .Value is not needed in any way in such a query and can just be dropped. But still, it is better to know what how it behaves with NHibernate.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It throws on v.Value evaluation:

    NHibernate.HibernateException : Evaluation failure on null.Value
  ----> System.InvalidOperationException : Nullable object must have a value.

Maybe we shouldn't try to evaluate it in this context. But it's a different story not related to this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sure, it would be beneficial if nullable.Value works (with the SQL parameter becoming false, true, or NULL). The value of supporting this would be that users don't have to remember to adapt their expression specially.

No idea what the effort would be to support it though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be beneficial if nullable.Value works

Just to clarify things. This PR fixes regression caused by optimizations made in #2948, #2957. nullableNullParam.HasValue or nullableNullParam.GetValueOrDefault() works in 5.3 but throws on master (5.4-dev).

nullable.Value fails the same in 5.3. So it's more like another feature request.

@hazzik hazzik merged commit c954e6b into nhibernate:master Sep 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Evaluation failure when using Nullable without a value in LINQ

4 participants